﻿Public Class Form2



    Public dstable As rankingDataTable = New rankingDataTable

    Public Sub load_file(ByVal filename As String)
        dstable.loadCSVFileIntoTable(filename)
        set_datatable(dstable)
        Dim openfileinfo As IO.FileInfo = New IO.FileInfo(filename)
        Me.Text = "Local file: " & openfileinfo.Name
    End Sub

    Public Sub load_web(ByVal startpage As Integer, ByVal endpage As Integer)
        dstable.loadrankingpagesfromweb(startpage, endpage)
        set_datatable(dstable)
    End Sub

    Public Sub set_datatable(db_table As DataTable)
        DataGridView1.DataSource = db_table
        setupDGview(DataGridView1, db_table)
    End Sub

    Public Function isloadfrom() As rankingDataTable.Loadedfrom
        Return dstable.IsLoadFrom
    End Function

    Private Sub setupDGview(ByRef DGview As DataGridView, ByRef Datat As DataTable)
        DGview.DataSource = Datat

        setupDGcolumn(DGview, "Position", True, False)
        setupDGcolumn(DGview, "Name", True, False)
        setupDGcolumn(DGview, "ID", True, True)
        setupDGcolumn(DGview, "Rank", True, True)
        setupDGcolumn(DGview, "Prestige", True, True)
        setupDGcolumn(DGview, "Additional info", False, False)
    End Sub

    Private Sub setupDGcolumn(ByRef dataGridView As DataGridView, ByVal column As String, ByVal locked As Boolean, ByVal rightalign As Boolean)
        With dataGridView.Columns(column)
            If rightalign Then
                '.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleRight

            End If
            '.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter
            '.ReadOnly = locked

        End With

    End Sub

    Private Sub DataGridView1_CellClick(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellClick

        If e.RowIndex > -1 Then

            With DataGridView1

                .CurrentCell = Me.DataGridView1(5, e.RowIndex)
                .Rows(e.RowIndex).Selected = True
                .BeginEdit(True)

            End With

        End If

    End Sub

    Public Sub saveTable2CSV(ByVal filename As String)
        dstable.saveTableToCSVFile(filename)
    End Sub



    Private Sub Button1_Click(sender As System.Object, e As System.EventArgs)
        DataGridView1.Sort(DataGridView1.Columns("Position"), System.ComponentModel.ListSortDirection.Ascending)
    End Sub

    Private Sub Form2_Load(sender As Object, e As System.EventArgs) Handles Me.Load


        AddHandler dstable.downloadingnewpage, AddressOf newpageinDL
        AddHandler dstable.loadingfinshed, AddressOf downloaddone

    End Sub

    Private Sub newpageinDL(page As Decimal)
        Dim parent As MDIParent1 = MDIParent1
        With parent
            .SetTSSlabel("Downloading page " & page.ToString)
        End With


    End Sub

    Private Sub downloaddone()
        Dim parent As MDIParent1 = MDIParent1
        With parent
            .SetTSSlabel("Done ..")
            .NewToolStripButton.Enabled = True
            .SaveToolStripButton.Enabled = True
            .PrintPreviewToolStripButton.Enabled = True
        End With

    End Sub

    Public Sub searchDataTable()
        Dim searchdialog As New Dialog2
        Dim searchcolomn As String = ""
        Dim searchstring As String
        Dim res_dt As DataTable = New DataTable

        If (searchdialog.ShowDialog(Me) = Windows.Forms.DialogResult.OK) Then

            searchstring = searchdialog.searchtext(searchcolomn)

            dstable.searchInDatatable(dstable, res_dt, searchcolomn, searchstring, True)

            Dim newchild As New Form2
            newchild.MdiParent = Me.MdiParent
            newchild.Show()
            newchild.Text = "Search results from searching: """ & searchstring & """ in colummn: " & searchcolomn
            newchild.set_datatable(res_dt)

        End If

    End Sub


End Class

